/**
 * 
 */
package com.swe727.spring2011.constraint1.server;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.rmi.RemoteException;

import com.swe727.spring2011.constraint1.client.NodeController;
import com.swe727.spring2011.constraint1.observation.Observation;
import com.swe727.spring2011.constraint1.observation.ObservationDataStore;



/**
 * The remote object instance for saving observations.
 * @author Trevor Wright
 *
 */
public class SaveObservationHandler implements SaveObservationRemote {

	private final ObservationDataStore STORE;
	private final String LOGFILE;
	
	/**
	 * Create a new handler which will save observations to the given store.
	 * @param dataStore store to which observations are saved.
	 */
	public SaveObservationHandler(ObservationDataStore dataStore,String logfile) {
		STORE = dataStore;
		LOGFILE = logfile;
	}
	
	/* (non-Javadoc)
	 * @see com.swe727.spring2011.constraint1.server.SaveObservation#saveObservation(com.swe727.spring2011.constraint1.observation.Observation)
	 */
	public void saveObservation(Observation obs) throws RemoteException {
		STORE.add(obs);
	}
	
	public void logMemoryFootprintOfServer() throws RemoteException {
		try
		{
			
			long memused = Runtime.getRuntime().totalMemory();
			String logmsg="memory used = "+memused+"\n";

			
			FileOutputStream fos = new FileOutputStream(LOGFILE, true);
			
			fos.write(logmsg.getBytes());
			
			fos.close();
		}
		catch(FileNotFoundException ex)
		{
			System.out.println("FileNotFoundException : " + ex);
		}
		catch(IOException ioe)
		{
			System.out.println("IOException : " + ioe);
		}

	}
}
